package com.umeng.analytics.pro;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.umeng.analytics.AnalyticsConfig;

import org.json.JSONObject;

import java.lang.reflect.Method;

public class bd {
    private static final String a = "session_start_time";
    private static final String b = "session_end_time";
    private static final String c = "session_id";
    private final String d = "a_start_time";
    private final String e = "a_end_time";
    private static String f = null;
    private static Context g = null;

    public bd() {
    }

    public static String a() {
        return bd.g(bd.g);
    }

    private String a(Context arg11, SharedPreferences arg12) {
        long v8 = 0;
        ar v0 = ar.b(arg11);
        String v1 = this.b(arg11);
        long v2 = System.currentTimeMillis();
        try {
            JSONObject v4_1 = new JSONObject();
            v4_1.put("__e", v2);
            w.a(bd.g).a(v1, v4_1, w.w$a.BEGIN);
        } catch (Throwable v4) {
        }

        this.a(arg11);
        SharedPreferences.Editor v4_2 = arg12.edit();
        v4_2.putString("session_id", v1);
        v4_2.putLong("session_start_time", System.currentTimeMillis());
        v4_2.putLong("session_end_time", v8);
        v4_2.putLong("a_start_time", v2);
        v4_2.putLong("a_end_time", v8);
        v4_2.putInt("versioncode", Integer.parseInt(bv.a(arg11)));
        v4_2.putString("versionname", bv.b(arg11));
        v4_2.commit();
        v0.a(Boolean.valueOf(true));
        return v1;
    }

    public boolean a(Context arg15) {
        boolean v0;
        long v12 = 0;
        SharedPreferences v3 = ba.a(arg15);
        String v4 = v3.getString("session_id", null);
        if (v4 == null) {
            v0 = false;
            return v0;
        }

        long v6 = v3.getLong("session_start_time", v12);
        long v8 = v3.getLong("session_end_time", v12);
        if (v8 != v12) {
            Math.abs(v8 - v6);
        }

        try {
            JSONObject v5 = new JSONObject();
            v5.put("__ii", v4);
            v5.put("__e", v6);
            v5.put("__f", v8);
            double[] v0_2 = AnalyticsConfig.getLocation();
            if (v0_2 != null) {
                JSONObject v6_1 = new JSONObject();
                v6_1.put("lat", v0_2[0]);
                v6_1.put("lng", v0_2[1]);
                v6_1.put("ts", System.currentTimeMillis());
                v5.put("__d", v6_1);
            }

            Class v0_3 = Class.forName("android.net.TrafficStats");
            Method v6_2 = v0_3.getMethod("getUidRxBytes", Integer.TYPE);
            Method v7 = v0_3.getMethod("getUidTxBytes", Integer.TYPE);
            int v8_1 = arg15.getApplicationInfo().uid;
            if (v8_1 == -1) {
                return false;
            }

            long v10 = ((Long) v6_2.invoke(null, Integer.valueOf(v8_1))).longValue();
            v6 = ((Long) v7.invoke(null, Integer.valueOf(v8_1))).longValue();
            if (v10 > v12 && v6 > v12) {
                JSONObject v0_4 = new JSONObject();
                v0_4.put("download_traffic", v10);
                v0_4.put("upload_traffic", v6);
                v5.put("__c", v0_4);
            }

            w.a(arg15).a(v4, v5, w.w$a.NEWSESSION);
            bf.a(bd.g);
            ap.b(bd.g);
            this.a(v3);
            v0 = true;
        } catch (Throwable v0_1) {
            v0 = false;
        }

        return v0;
    }

    private void a(SharedPreferences arg3) {
        SharedPreferences.Editor v0 = arg3.edit();
        v0.remove("session_start_time");
        v0.remove("session_end_time");
        v0.remove("a_start_time");
        v0.remove("a_end_time");
        v0.commit();
    }

    public String b(Context arg6) {
        String v0 = bv.c(arg6);
        String v1 = AnalyticsConfig.getAppkey(arg6);
        long v2 = System.currentTimeMillis();
        if (v1 == null) {
            throw new RuntimeException("Appkey is null or empty, Please check AndroidManifest.xml");
        }

        StringBuilder v4 = new StringBuilder();
        v4.append(v2).append(v1).append(v0);
        v0 = bw.a(v4.toString());
        bd.f = v0;
        return v0;
    }

    private boolean b(SharedPreferences arg13) {
        boolean v0 = false;
        long v10 = 0;
        long v2 = arg13.getLong("a_start_time", v10);
        long v4 = arg13.getLong("a_end_time", v10);
        long v6 = System.currentTimeMillis();
        if (v2 != v10 && v6 - v2 < AnalyticsConfig.kContinueSessionMillis) {
            by.e("onResume called before onPause");
            return v0;
        }

        if (v6 - v4 > AnalyticsConfig.kContinueSessionMillis) {
            String v2_1 = bd.a();
            if (!TextUtils.isEmpty(((CharSequence) v2_1))) {
                long v0_1 = arg13.getLong("session_end_time", v10);
                if (v0_1 == v10) {
                    v0_1 = System.currentTimeMillis();
                }

                try {
                    JSONObject v3 = new JSONObject();
                    v3.put("__f", v0_1);
                    w.a(bd.g).a(v2_1, v3, w.w$a.END);
                } catch (Throwable v0_2) {
                }
            }

            v0 = true;
        }

        return v0;
    }

    public void c(Context arg7) {
        bd.g = arg7;
        SharedPreferences v0 = ba.a(arg7);
        if (v0 != null) {
            SharedPreferences.Editor v1 = v0.edit();
            int v2 = v0.getInt("versioncode", 0);
            int v3 = Integer.parseInt(bv.a(bd.g));
            if (v2 != 0 && v3 != v2) {
                try {
                    v1.putInt("vers_code", v2);
                    v1.putString("vers_name", v0.getString("versionname", ""));
                    v1.commit();
                } catch (Throwable v1_1) {
                }

                if (bd.g(arg7) == null) {
                    this.a(arg7, v0);
                }

                this.e(bd.g);
                ar.b(bd.g).b();
                this.f(bd.g);
                ar.b(bd.g).a();
                return;
            }

            if (this.b(v0)) {
                by.c("Start new session: " + this.a(arg7, v0));
            } else {
                String v0_1 = v0.getString("session_id", null);
                v1.putLong("a_start_time", System.currentTimeMillis());
                v1.putLong("a_end_time", 0);
                v1.commit();
                by.c("Extend current session: " + v0_1);
            }
        }
    }

    public void d(Context arg7) {
        long v4 = 0;
        SharedPreferences v0 = ba.a(arg7);
        if (v0 != null) {
            if (v0.getLong("a_start_time", v4) == v4 && (AnalyticsConfig.ACTIVITY_DURATION_OPEN)) {
                by.e("onPause called before onResume");
                return;
            }

            long v2 = System.currentTimeMillis();
            SharedPreferences.Editor v0_1 = v0.edit();
            v0_1.putLong("a_start_time", v4);
            v0_1.putLong("a_end_time", v2);
            v0_1.putLong("session_end_time", v2);
            v0_1.commit();
        }
    }

    public boolean e(Context arg9) {
        boolean v0 = false;
        long v6 = 0;
        SharedPreferences v1 = ba.a(arg9);
        if (v1 != null && v1.getString("session_id", null) != null) {
            long v2 = v1.getLong("a_start_time", v6);
            long v4 = v1.getLong("a_end_time", v6);
            if (v2 > v6 && v4 == v6) {
                v0 = true;
                this.d(arg9);
            }

            v2 = v1.getLong("session_end_time", v6);
            try {
                JSONObject v1_2 = new JSONObject();
                String v4_1 = "__f";
                if (v2 == v6) {
                    v2 = System.currentTimeMillis();
                }

                v1_2.put(v4_1, v2);
                w.a(bd.g).a(bd.a(), v1_2, w.w$a.END);
            } catch (Throwable v1_1) {
            }

            this.a(arg9);
        }

        return v0;
    }

    public void f(Context arg11) {
        long v8 = 0;
        SharedPreferences v0 = ba.a(arg11);
        if (v0 != null) {
            String v1 = this.b(arg11);
            SharedPreferences.Editor v0_1 = v0.edit();
            long v2 = System.currentTimeMillis();
            v0_1.putString("session_id", v1);
            v0_1.putLong("session_start_time", System.currentTimeMillis());
            v0_1.putLong("session_end_time", v8);
            v0_1.putLong("a_start_time", v2);
            v0_1.putLong("a_end_time", v8);
            v0_1.putInt("versioncode", Integer.parseInt(bv.a(arg11)));
            v0_1.putString("versionname", bv.b(arg11));
            try {
                JSONObject v4 = new JSONObject();
                v4.put("__e", v2);
                w.a(bd.g).a(v1, v4, w.w$a.BEGIN);
            } catch (Throwable v1_1) {
            }
            v0_1.commit();
        }
    }

    public static String g(Context arg3) {
        if (bd.f == null) {
            bd.f = ba.a(arg3).getString("session_id", null);
        }

        return bd.f;
    }
}

